<?php
//测试控制器类
class IndexController extends CommonController{
    //动作方法
    private $model2;

    public function __init(){
    	parent::__init();
    	$this->model2 = K('District');
    }
    
    public function index(){
    	$abcde = 'ABCDE';
    	$fghij = 'FGHIJ';
    	$klmno = 'KLMNO';
    	$pqrst = 'PQRST';

    	// 获得所有省份
       $province = $this->model2->get_data(array('pid'=>0));
       $a = array();
       $f = array();
       $k = array();
       $p = array();
       foreach ($province as $v) {
       	if(strpos($abcde,$v['ucwords'])){
       		$a[] = $v;
       	}elseif (strpos($fghij,$v['ucwords'])) {
       		$f[] = $v;
       	}elseif (strpos($klmno,$v['ucwords'])) {
       		$k[] = $v;
       	}elseif (strpos($pqrst,$v['ucwords'])) {
       		$p[] = $v;
       	}
       }
	 $this->assign('a',$a);
       $this->assign('f',$f);
       $this->assign('k',$k);
       $this->assign('p',$p);
        $this->display();
    }


    /**
     * [ajax_district 异步子集区域显示]
     * @return [type] [description]
     */
    public function ajax_district(){
    	if(IS_AJAX){
    		$did = Q('post.did');
    		$allData = $this->model2->get_data();
    		$son_data = Data::parentChannel($allData, $did, $fieldPri ='pid', $fieldPid='did');
    		$reseData = array();



       /**
        * [$key 地区数据重组]
        * @var [type]
        */
    		foreach ($son_data  as $key => $value) {

          foreach ($son_data as $v) {
           
             if($value['did']==$v['pid']){
                $value['son'][] = $v;
             }
             
           }
           if($value['pid'] == $did){
            $reseData[] = $value;
           }
      }
    		$this->ajax($reseData,'JSON');
    		

    	}
    }

    /**
     * [ajax_inquire 异步搜索显示]
     * @return [type] [description]
     */
    public function ajax_inquire(){
          $did = Q('post.did');
          $value = Q('post.value');
          $allData = $this->model2->get_data();
          $son_data = Data::parentChannel($allData, $did, $fieldPri ='pid', $fieldPid='did');

          $arrValue = String::splitWord($value);
         
          $district = ''.$did.'';

          if($son_data){
            $district = ''.$did.',';
          }
          foreach ($son_data as $key => $value) {
            $district .= ''.$value['did'].',';
          }

          $district = rtrim($district,',');
          $where ="did IN ( ". $district . ") AND ";
          if($arrValue){
              if(count($arrValue)<2){
                foreach ($arrValue as $key => $value) {
            $where .= "storesite LIKE '%".$key."%'"; 
            }
              }else{
                foreach ($arrValue as $key => $value) {
            $where .= "storesite LIKE '%".$key."%' or "; 
            }

              }
            
          }
          

           $NewData = K('Store')->where($where)->all();

          
           $this->ajax($NewData);
        }

 //    public function get_son($cate,$did){
	// 	$arr = array();
	// 	foreach ($cate as $v) {
			
	// 		//判断  如果 $v里面的cid == 当前的cid;那么它是当前分类
	// 		if($v['pid'] == $did){
	// 			//把它压倒空数据里面
	// 			$arr[] = $v;

	// 			//然后查找 当前当前pic 所对应的父级的CID.
	// 			$arr = array_merge($arr,$this->get_son($cate,$v['did'])); 
	// 		}
	// 	}
	// 	return $arr;
	// }
}
